﻿<UserControl x:Class="GSF.TimeSeries.UI.UserControls.AdapterUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="650" d:DesignWidth="850"
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:uc="clr-namespace:GSF.TimeSeries.UI.UserControls"
             xmlns:tsfBinding="clr-namespace:GSF.TimeSeries.UI.Binding;assembly=GSF.TimeSeries.UI"
             xmlns:vm="clr-namespace:GSF.TimeSeries.UI.ViewModels"
             Background="Transparent">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/GSF.TimeSeries.UI.WPF;Component/Resources/CommonResource.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Vertical">

        <StackPanel.Resources>
            <CollectionViewSource Source="{Binding Path=NodeLookupList}" x:Key="NodeLookupList"/>
            <CollectionViewSource Source="{Binding Path=AdapterTypeList}" x:Key="AdapterTypeList"/>
            <CollectionViewSource Source="{Binding Path=ParameterList}" x:Key="ParameterList"/>
        </StackPanel.Resources>

        <Grid x:Name="GridDetailView" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" DataContext="{Binding Path=CurrentItem, Mode=TwoWay}" DataContextChanged="GridDetailView_DataContextChanged">
            <Grid.BindingGroup >
                <BindingGroup Name="input"/>
            </Grid.BindingGroup>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>



            <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right">
                <TextBlock Text="Name" Style="{StaticResource RequiredLabel}" />
                <TextBlock Style="{StaticResource RequiredMarker}" />
            </StackPanel>
            <TextBox Grid.Row="0" Grid.Column="1" CharacterCasing="Upper" Text="{tsfBinding:Column Path=AdapterName}" MaxLength="50"/>

            <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right">
                <TextBlock Text="Load Order" Style="{StaticResource RequiredLabel}" />
                <TextBlock Style="{StaticResource RequiredMarker}" />
            </StackPanel>
            <TextBox Grid.Row="0" Grid.Column="3" Text="{tsfBinding:Column Path=LoadOrder}" Width="35" />
            <GroupBox Grid.Row="2" Grid.ColumnSpan="4" Header="Adapter Type">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Search Directory" />
                    <StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Orientation="Horizontal">
                        <TextBox DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext}"
                                 Name="SearchDirectoryTextBox" Text="{tsfBinding:Column Path=SearchDirectory}" Width="525" />
                        <Button Click="Browse_Click" Width="Auto" ToolTip="Browse">
                            <Button.Template>
                                <ControlTemplate>
                                    <Image Source="/GSF.TimeSeries.UI;component/images/Folder.png" Height="24" Width="24" />
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </StackPanel>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Type" />
                    <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Source={StaticResource AdapterTypeList}}" DisplayMemberPath="Value" HorizontalContentAlignment="Left" Width="560"
                              SelectedItem="{tsfBinding:Column Path=TypeName, Converter={StaticResource DictionaryToKeyValuePairConverter}, ConverterParameter={StaticResource AdapterTypeList}}" />
                </Grid>
            </GroupBox>
            <GroupBox Grid.Row="3" Grid.ColumnSpan="4" Header="Connection String">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <TextBlock Text="Parameters" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0" />

                    <ListBox Grid.Row="0" Grid.Column="1" Width="{StaticResource XLargeItemSize}" Height="{StaticResource XSmallItemSize}" HorizontalContentAlignment="Stretch"
                             Name="ParameterListBox" ItemsSource="{Binding Source={StaticResource ParameterList}}" SelectedItem="{Binding Path=SelectedParameter}"
                             DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext}" ScrollViewer.VerticalScrollBarVisibility="Auto"
                             ScrollViewer.HorizontalScrollBarVisibility="Auto">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock FontWeight="{Binding Path=Boldness}" Foreground="{Binding Path=Color}" Text="{Binding Path=Name}" ToolTipService.InitialShowDelay="0" Width="Auto" TextAlignment="Left" HorizontalAlignment="Left">
                                    <TextBlock.ToolTip>
                                        <ToolTip>
                                            <TextBlock Text="{Binding Path=Description}" />
                                        </ToolTip>
                                    </TextBlock.ToolTip>
                                </TextBlock>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <StackPanel Grid.Row="0" Grid.Column="2" Margin="10,5,0,0">
                        <TextBlock Name="ParameterValueTextBlock" Margin="0" Text="Value" HorizontalAlignment="Center" VerticalAlignment="Top" />
                        <Button Content="Default" Margin="0" Width="Auto" HorizontalAlignment="Center" ToolTip="Click to restore the default value." Click="Default_Click" />
                    </StackPanel>
                    <StackPanel Grid.Row="0" Grid.Column="3" DataContext="{Binding ElementName=ParameterListBox, Path=SelectedItem}">
                        <StackPanel.Resources>
                            <CollectionViewSource Source="{Binding Path=EnumValues}" x:Key="EnumValues" />
                        </StackPanel.Resources>

                        <TextBox Name="ParameterValueTextBox" Width="{StaticResource XLargeItemSize}" Height="{StaticResource XSmallItemSize}" Text="{tsfBinding:Column Path=Value}" TextWrapping="Wrap"
                                 Visibility="{Binding Path=IsOther, Converter={StaticResource ObjectToVisibilityConverter}}" VerticalScrollBarVisibility="Auto"/>

                        <StackPanel>
                            <StackPanel.Style>
                                <Style TargetType="{x:Type StackPanel}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ElementName=ParameterListBox, Path=SelectedItem}" Value="{x:Null}">
                                            <Setter Property="Visibility" Value="Collapsed" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>

                            <StackPanel Margin="5" Width="{Binding ElementName=ParameterValueTextBox, Path=Width}" Height="{Binding ElementName=ParameterValueTextBox, Path=Height}"
                                        Visibility="{Binding Path=IsBoolean, Converter={StaticResource ObjectToVisibilityConverter}}">
                                <RadioButton Content="True" GroupName="BooleanParameter" IsChecked="{Binding Path=Value}" />
                                <RadioButton Content="False" GroupName="BooleanParameter" IsChecked="{Binding Mode=OneWay, Path=IsFalseChecked}" />
                            </StackPanel>

                            <StackPanel Margin="5" Height="{Binding ElementName=ParameterValueTextBox, Path=Height}"
                                        Visibility="{Binding Path=IsEnum, Converter={StaticResource ObjectToVisibilityConverter}}">
                                <ComboBox ItemsSource="{Binding Source={StaticResource EnumValues}}" SelectedItem="{Binding Path=Value}" />
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Connection String" VerticalAlignment="Top" />
                    <TextBox Grid.Row="1" Grid.Column="1"  Grid.ColumnSpan="3" Width="560" Height="{StaticResource XSmallItemSize}" HorizontalAlignment="Stretch"
                             Text="{tsfBinding:Column Path=ConnectionString}" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Visible" />
                </Grid>
            </GroupBox>
            <TextBlock Text="Runtime ID" Grid.Row="4" Grid.Column="0" />
            <StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1">
                <TextBlock Text="{tsfBinding:Column Path=DataContext.RuntimeID, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}" />
                <Button Content="Initialize" Command="{Binding Path=DataContext.InitializeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}" 
                        IsEnabled="{Binding Path=DataContext.IsNewRecord, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}, Converter={StaticResource NotConverter}}"/>
            </StackPanel>
            <CheckBox Content="Enabled" Grid.Row="4" Grid.Column="3" IsChecked="{tsfBinding:Column Path=Enabled}"/>
        </Grid>

        <uc:DetailViewFooterUserControl x:Name="UserControlDetailViewFooter" DeleteCommand="{Binding DeleteCommand}" ClearCommand="{Binding ClearCommand}" SaveCommand="{Binding SaveCommand}"
                SaveEnabled="{tsfBinding:DataOperation Path=(Validation.HasError)}" />

        <sdk:DataGrid x:Name="DataGridList" ItemsSource="{Binding CurrentPage}" SelectedItem="{Binding Path=CurrentItem, Mode=TwoWay}" Height="Auto" PreviewKeyDown="DataGrid_PreviewKeyDown" Sorting="DataGrid_Sorting"
                      SelectedIndex="{tsfBinding:Column Path=CurrentSelectedIndex}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="Name" Width="*" Binding="{tsfBinding:Column Path=AdapterName}"/>
                <sdk:DataGridTextColumn Header="Assembly Name" Width="*" Binding="{tsfBinding:Column Path=AssemblyName}" />
                <sdk:DataGridTextColumn Header="Type Name" Width="2*" Binding="{tsfBinding:Column Path=TypeName}" />
                <sdk:DataGridCheckBoxColumn Header="Enabled" Width="*" Binding="{tsfBinding:Column Path=Enabled}" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

        <uc:DataPagerUserControl CurrentPageNumber="{Binding CurrentPageNumber}" PageCount="{Binding PageCount}"
                                 FirstCommand="{Binding FirstCommand}" PreviousCommand="{Binding PreviousCommand}"
                                 NextCommand="{Binding NextCommand}" LastCommand="{Binding LastCommand}"/>
    </StackPanel>
</UserControl>
